package org.huel.backend.service.impl;

import org.huel.backend.domain.dto.AttendanceSearchDTO;
import org.huel.backend.domain.dto.AttendanceStatsDTO;
import org.huel.backend.domain.dto.StudentAttendanceDTO;
import org.huel.backend.domain.entity.Attendance;
import org.huel.backend.domain.entity.ClassAttendance;
import org.huel.backend.domain.entity.CourseAttendance;
import org.huel.backend.mapper.AttendanceMapper;
import org.huel.backend.mapper.ClassAttendanceMapper;
import org.huel.backend.mapper.CourseAttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    /**
     * 搜索出勤记录
     */
    public Map<String, Object> searchAttendance(AttendanceSearchDTO searchDTO) {
        List<Attendance> records;
        Long total;

        Integer offset = searchDTO.getOffset();
        Integer pageSize = searchDTO.getPageSize();

        // 根据不同条件调用不同方法
        if (searchDTO.getClassId() != null && !searchDTO.getClassId().isEmpty()) {
            records = attendanceMapper.searchAttendanceByClass(searchDTO.getClassId(), offset, pageSize);
            total = attendanceMapper.countAttendanceByClass(searchDTO.getClassId());
        } else if (searchDTO.getCourseName() != null && !searchDTO.getCourseName().isEmpty()) {
            records = attendanceMapper.searchAttendanceByCourse(searchDTO.getCourseName(), offset, pageSize);
            total = attendanceMapper.countAttendanceByCourse(searchDTO.getCourseName());
        } else if (searchDTO.getStudentName() != null && !searchDTO.getStudentName().isEmpty()) {
            records = attendanceMapper.searchAttendanceByStudentName(searchDTO.getStudentName(), offset, pageSize);
            total = attendanceMapper.countAttendanceByStudentName(searchDTO.getStudentName());
        } else if (searchDTO.getIsAbsent() != null) {
            records = attendanceMapper.searchAttendanceByStatus(searchDTO.getIsAbsent(), offset, pageSize);
            total = attendanceMapper.countAttendanceByStatus(searchDTO.getIsAbsent());
        } else {
            records = attendanceMapper.searchAttendanceBasic(offset, pageSize);
            total = attendanceMapper.countAttendanceBasic();
        }

        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("page", searchDTO.getPage());
        result.put("pageSize", searchDTO.getPageSize());
        result.put("totalPages", (total + searchDTO.getPageSize() - 1) / searchDTO.getPageSize());

        return result;
    }

    /**
     * 获取所有班级ID
     */
    public List<String> getAllClassIds() {
        return attendanceMapper.getAllClassIds();
    }

    /**
     * 获取所有课程名称
     */
    public List<String> getAllCourseNames() {
        return attendanceMapper.getAllCourseNames();
    }

    /**
     * 获取所有学生姓名
     */
    public List<String> getAllStudentNames() {
        return attendanceMapper.getAllStudentNames();
    }

    /**
     * 获取学生出勤统计
     */
    public List<AttendanceStatsDTO> getStudentAttendanceStats(AttendanceSearchDTO searchDTO) {
        List<AttendanceStatsDTO> stats = attendanceMapper.getStudentAttendanceStats();
        // 计算出勤率
        for (AttendanceStatsDTO stat : stats) {
            stat.calculateAttendanceRate();
        }
        return stats;
    }

    /**
     * 获取课程出勤统计
     */
    public List<AttendanceStatsDTO> getCourseAttendanceStats(AttendanceSearchDTO searchDTO) {
        List<AttendanceStatsDTO> stats = attendanceMapper.getCourseAttendanceStats();
        for (AttendanceStatsDTO stat : stats) {
            stat.calculateAttendanceRate();
        }
        return stats;
    }

    /**
     * 获取班级出勤统计
     */
    public List<AttendanceStatsDTO> getClassAttendanceStats(AttendanceSearchDTO searchDTO) {
        List<AttendanceStatsDTO> stats = attendanceMapper.getClassAttendanceStats();
        for (AttendanceStatsDTO stat : stats) {
            stat.calculateAttendanceRate();
        }
        return stats;
    }

    /**
     * 获取整体出勤统计摘要
     */
    public Map<String, Object> getAttendanceSummary(AttendanceSearchDTO searchDTO) {
        Long total = attendanceMapper.countAttendanceBasic();
        Long absentCount = attendanceMapper.countAttendanceByStatus(1);
        Long presentCount = total - absentCount;

        Double attendanceRate = total > 0 ? (presentCount.doubleValue() / total.doubleValue() * 100) : 0.0;

        Map<String, Object> summary = new HashMap<>();
        summary.put("totalCount", total);
        summary.put("presentCount", presentCount);
        summary.put("absentCount", absentCount);
        summary.put("attendanceRate", Math.round(attendanceRate * 100.0) / 100.0);

        return summary;
    }

}